package ustc.xiaoyang.karl.linkedlist;

/**
 * 142. 环形链表 II
 */
public class CircularLinkedList142 {
    public static void main(String[] args) {

    }
    public ListNode detectCycle(ListNode head){
        ListNode slow = head , fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast){
                slow = head;
                while(slow != fast){
                    slow = slow.next;
                    fast = fast.next;
                }
                return slow;
            }
        }
        return null;
    }
}
